import { get, post } from './request'

/**
 * 通用API模块 - 包含系统常用API
 */

// 获取系统配置
const getSystemConfig = () => {
  return get('/system/config')
}

// 获取字典数据
const getDictionary = (type) => {
  return get('/system/dict', { type })
}

// 用户登录
const login = (username, password) => {
  return post('/auth/login', { username, password })
}

// 用户登出
const logout = () => {
  return post('/auth/logout')
}

// 获取当前用户信息
const getUserInfo = () => {
  return get('/user/info')
}

// 重置密码
const resetPassword = (data) => {
  return post('/user/reset-password', data)
}

// 上传文件
const uploadFile = (formData, onProgress) => {
  // 这里特殊处理，使用axios实例直接上传
  return post('/file/upload', formData, false, {
    headers: {
      'Content-Type': 'multipart/form-data'
    },
    onUploadProgress: onProgress
  })
}

// 通用的动态API请求函数
const request = (method, url, data = {}, useRootPath = true) => {
  const lowerMethod = method.toLowerCase()
  
  if (lowerMethod === 'get' || lowerMethod === 'delete') {
    return get(url, data, useRootPath)
  } else if (lowerMethod === 'post' || lowerMethod === 'put') {
    return post(url, data, useRootPath)
  } else {
    throw new Error(`不支持的HTTP方法: ${method}`)
  }
}

export {
  getSystemConfig,
  getDictionary,
  login,
  logout,
  getUserInfo,
  resetPassword,
  uploadFile,
  request
} 